
//插件模块引入
var colorAdminApp = angular.module('colorAdminApp', [
    'ui.router',
    'ui.bootstrap',
    'ngCookies',
    'oc.lazyLoad',
    'easy.ng.pagination'
]);

/**
 * 确保HttpInterceptor中能取到$rootScope,$state,$stateParams
 */
colorAdminApp.run(['$rootScope','$state','$stateParams',function($rootScope,$state,$stateParams){
	$rootScope.$state=$state;
	$rootScope.$stateParams=$stateParams;
}]);




//配置为正常的http请求而不是包装后的请求
colorAdminApp.config(function($httpProvider) {
	//此处的Authorization用户名zuul_server与密码secret的base64加密，要与OAuthConfiguration中的.withClient("zuul_server").secret("secret")
	//方法设置的一置
	$httpProvider.defaults.headers.common={'Authorization' :'Basic enV1bF9zZXJ2ZXI6c2VjcmV0' }
    $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
    $httpProvider.defaults.headers.post['X-Requested-With']='XMLHttpRequest';//让后台可以识别是异步请求
    $httpProvider.defaults.headers.put['X-Requested-With']='XMLHttpRequest';//让后台可以识别是异步请求
    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
            var param = function(obj) {
            var query = '';
            var name, value, fullSubName, subName, subValue, innerObj, i;
 
            for (name in obj) {
                value = obj[name];
 
                if (value instanceof Array) {
                    for (i = 0; i < value.length; ++i) {
                        subValue = value[i];
                        fullSubName = name + '[' + i + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value instanceof Object) {
                   for (subName in value) {
                        subValue = value[subName];
                        innerObj = {};
                        innerObj[subName] = subValue;//此处实现和jquery post方法提交值相同的方式
                        query += param(innerObj) + '&';
                    }
                	
                } else if (value !== undefined && value !== null) {
                    query += encodeURIComponent(name)+'='+ encodeURIComponent(value) + '&';
                }
            }
 
            return query.length ? query.substr(0, query.length - 1) : query;
        };
        return angular.isObject(data) && String(data) !== '[object File]'? param(data): data;
    }];
});

//配置可以动态加载控制器
colorAdminApp.config(["$provide", "$compileProvider", "$controllerProvider", "$filterProvider",
           
    function ($provide, $compileProvider, $controllerProvider, $filterProvider) {
       
     colorAdminApp.controller = $controllerProvider.register;
       
     colorAdminApp.directive = $compileProvider.directive;
       
     colorAdminApp.filter = $filterProvider.register;
       
     colorAdminApp.factory = $provide.factory;
       
     colorAdminApp.service = $provide.service;
       
     colorAdminApp.constant = $provide.constant;
       
}]);



//定义一个 Service ，稍等将会把它作为 Interceptors 的处理函数
colorAdminApp.factory('HttpInterceptor', ['$q','$injector','$cookieStore', HttpInterceptor]);


//请求过滤器
function HttpInterceptor($q,$injector,$cookieStore) {
  var rootScope=$injector.get('$rootScope');
  return {
    request: function(config){
       //从cookie中取出access_token放到请求参数中	
      if($cookieStore.get("access_token")!=undefined&&config["data"]!=undefined&&config["data"]["params"]!=undefined){
       	 config["data"]["params"]["access_token"]=$cookieStore.get("access_token");
    	 return config || $q.when(config);
      }	
      
      return config;
    },
    requestError: function(err){
      return $q.reject(err);
    },
    response: function(res){
    	return res;
    },
    responseError: function(err){
       //跳转到登录页面
       if(err.status==401){
    	  rootScope.$state.go("login",{"code":err.status});
    	  return;
       }
          //跳转到错误页面
          rootScope.$state.go("errors",{"code":err.status});
       return $q.reject(err);
    }
  };
}
// 添加对应的 Interceptors
colorAdminApp.config(['$httpProvider', function($httpProvider){
  $httpProvider.interceptors.push(HttpInterceptor);
}]);
colorAdminApp.controller('pageLoaderController', function($scope, $rootScope, $state) {
	App.initPageLoad();
});


colorAdminApp.config(['$stateProvider', '$urlRouterProvider', function($stateProvider,$urlRouterProvider) {
	
    $urlRouterProvider.otherwise('/login');
    
    //公共资源
    let MODAL_SERVICE='resources/js/service/ModalService.js';
    let DATA_TABLES_CSS='assets/plugins/DataTables/media/css/dataTables.bootstrap.min.css';
    let BOOTSTRAP_CSS='assets/plugins/DataTables/extensions/Responsive/css/responsive.bootstrap.min.css';
    let ZTREE_CSS='plugin/ztree/css/zTreeStyle/zTreeStyle.css';
    let ZTREE_CHECK_CSS='plugin/ztree/js/jquery.ztree.excheck-3.5.js';
    let ZTREE_EDIT_CSS='plugin/ztree/js/jquery.ztree.exedit-3.5.js';
    let BLOCK_UI='plugin/blockui/jquery.blockUI.js';
    let FILE_UPLOAD_BUTTON_CSS='resources/css/buttons/jquery.fileupload.css';
    let BOOTSTRAP_DATETIMEPICKER_CSS="plugin/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css";
    let BOOTSTRAP_DATETIMEPICKER="plugin/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js";
    let TAG_IT_JS='assets/plugins/jquery-tag-it/js/tag-it.js';
    let TAG_IT_CSS='assets/plugins/jquery-tag-it/css/jquery.tagit.css';
    let ISOTOPE_CSS='assets/plugins/isotope/isotope.css';
    
  
    //登录页面
    //路由ID，不可能重复
    $stateProvider.state("login",{
    	//路由访问路径
        url: "/login",
        //模版路径
        templateUrl: "login.html",
        //控制器名称
        controller:"loginController",
        resolve: {
            deps:["$ocLazyLoad",function($ocLazyLoad){
            	  //控制器JS路径
                  return $ocLazyLoad.load("resources/js/controller/LoginController.js");
                  }],
            service: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                	//其它需要使用的资源文件
                    files: ['resources/js/service/LoginService.js'
                    ] 
                });
            }]
        }
    });
    
      //登录后页面
      $stateProvider.state("main",{
        url: "/main",
        controller:"mainController",
        templateUrl: "view/main.html",
        resolve: {
            deps:["$ocLazyLoad",function($ocLazyLoad){
                  return $ocLazyLoad.load("resources/js/controller/MainController.js");
                  }],
            service: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load({
                    files: [
                    ] 
                });
            }]
        }
    });
      
    $stateProvider.state("main.userPasswordUpdate",{
          url: "/core/access/user/passwordUpdate",
          controller:"passwordUpdateController",
          templateUrl:"view/core/access/password_update.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/access/PasswordUpdateController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    'resources/js/service/core/PasswordService.js'
                            ] 
                        });
              }]
          }
      });
      
      $stateProvider.state("main.userInfoUpdate",{
          url: "/core/user/infoUpdate",
          controller:"userInfoUpdateController",
          templateUrl:"view/core/user/user_info_update.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/user/UserInfoUpdateController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE
                            ] 
                        });
              }]
          }
      });
      
      $stateProvider.state("userPasswordUpdateForExpired",{
          url: "/core/user/passwordUpdateForExpired/:username",
          controller:"passwordUpdateForExpiredController",
          templateUrl:"view/core/user/password_update_for_expired.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/user/PasswordUpdateForExpiredController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    'resources/js/service/LoginService.js',
                                    'resources/js/service/core/PasswordService.js'
                            ] 
                        });
              }]
          }
      });
      
      //错误信息页面
      $stateProvider.state("errors",{
          url: "/errors/:code",
          controller:"errorsController",
          templateUrl:"view/errors/error.html", 
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                  return $ocLazyLoad.load("resources/js/controller/errors/ErrorsController.js");
                  }]
        }
      });
            
      //序列管理页面
      $stateProvider.state("main.SEQUENCE_MANAGE",{
          url: "/core/configuration/sequence/manage",
          controller:"sequenceController",
          templateUrl:"view/core/configuration/sequence/manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/configuration/SequenceController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      //主键管理页面
      $stateProvider.state("main.ID_GENERATOR_MANAGE",{
          url: "/core/configuration/idGenerator/manage",
          controller:"idGeneratorController",
          templateUrl:"view/core/configuration/idGenerator/manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/configuration/IdGeneratorController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      //角色管理页面
      $stateProvider.state("main.ROLE_MANAGE",{
          url: "/core/access/role/manage",
          controller:"roleController",
          templateUrl:"view/core/access/role_manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/access/RoleController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    ZTREE_CSS,
                                    ZTREE_CHECK_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      //组织机构（部门）管理页面
      $stateProvider.state("main.DEPARTMENT_MANAGE",{
          url: "/core/access/department/manage",
          controller:"departmentManageController",
          templateUrl:"view/core/access/department_manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/access/DepartmentManageController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    ZTREE_CSS,
                                    ZTREE_CHECK_CSS,
                                    ZTREE_EDIT_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      
      //菜单管理页面
      $stateProvider.state("main.MENU_MANAGE",{
          url: "/core/access/resources/:type/manage",
          controller:"resourcesManageController",
          templateUrl:"view/core/access/resources_manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/access/ResourcesManageController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    ZTREE_CSS,
                                    ZTREE_CHECK_CSS,
                                    ZTREE_EDIT_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      //功能管理页面
      $stateProvider.state("main.FUNCTION_MANAGE",{
          url: "/core/access/resources/:type/manage",
          controller:"resourcesManageController",
          templateUrl:"view/core/access/resources_manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/access/ResourcesManageController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    ZTREE_CSS,
                                    ZTREE_CHECK_CSS,
                                    ZTREE_EDIT_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      //用户管理页面
      $stateProvider.state("main.USER_MANAGE",{
          url: "/core/access/user/manage",
          controller:"userManageController",
          templateUrl:"view/core/access/user_manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/core/access/UserManageController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    ZTREE_CSS,
                                    ZTREE_CHECK_CSS,
                                    ZTREE_EDIT_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      //流程定义管理页面
      $stateProvider.state("main.WORKFLOW_PROCESS_DEFINITION_MANAGE",{
          url: "/workflow/process/definition/manage",
          controller:"processDefinitionController",
          templateUrl:"view/workflow/process_definition_manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/ProcessDefinitionController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      
      //流程实例管理页面
      $stateProvider.state("main.WORKFLOW_PROCESS_INSTANCE_MANAGE",{
          url: "/workflow/process/instance/manage",
          controller:"processInstanceController",
          templateUrl:"view/workflow/process_instance_manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/ProcessInstanceController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      
      //经办流程实页面
      $stateProvider.state("main.WORKFLOW_PERSONAL_PROCESS_INSTANCE",{
          url: "/workflow/process/instance/personal",
          controller:"personalProcessInstanceController",
          templateUrl:"view/workflow/personal_process_instance.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/PersonalProcessInstanceController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      
      
      //流程任务管理页面
      $stateProvider.state("main.WORKFLOW_PROCESS_TASK_MANAGE",{
          url: "/workflow/process/task/manage",
          controller:"processTaskController",
          templateUrl:"view/workflow/process_task_manage.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/ProcessTaskController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      
      //待办任务页面
      $stateProvider.state("main.WORKFLOW_PROCESS_TASK_ASSIGNEE",{
          url: "/workflow/process/task/assignee",
          controller:"assigneeProcessTaskController",
          templateUrl:"view/workflow/assignee_process_task.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/AssigneeProcessTaskController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      //候选用户任务
      $stateProvider.state("main.WORKFLOW_PROCESS_TASK_CANDIDATE_USER",{
          url: "/workflow/process/task/candidate/user",
          controller:"candidateUserProcessTaskController",
          templateUrl:"view/workflow/candidate_user_process_task.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/CandidateUserProcessTaskController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      //候选组任务页面
      $stateProvider.state("main.WORKFLOW_PROCESS_TASK_CANDIDATE_GROUP",{
          url: "/workflow/process/task/candidate/group",
          controller:"candidateGroupProcessTaskController",
          templateUrl:"view/workflow/candidate_group_process_task.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/CandidateGroupProcessTaskController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      //历史任务页面
      $stateProvider.state("main.WORKFLOW_PROCESS_TASK_HISTORIC_TASK_INSTANCE",{
          url: "/workflow/process/task/historic/:processInstanceId",
          controller:"historicTaskInstanceController",
          templateUrl:"view/workflow/historic_task_instance.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/HistoricTaskInstanceController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      //个人历史任务页面
      $stateProvider.state("main.WORKFLOW_PROCESS_TASK_PERSONAL_HISTORIC_TASK_INSTANCE",{
          url: "/workflow/process/task/historic/personal/:processInstanceId",
          controller:"personalHistoricTaskInstanceController",
          templateUrl:"view/workflow/personal_historic_task_instance.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/PersonalHistoricTaskInstanceController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    DATA_TABLES_CSS,
                                    BOOTSTRAP_CSS,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      
      
//-------------工作流示例页面开始--------------------//     
      
      //面试流程发起页面
      $stateProvider.state("main.INTERVIEW_START",{
          url: "/workflow/process/instance/interview/start/:processDefinitionId",
          controller:"interviewStartController",
          templateUrl:"view/workflow/form/interview/interview_start.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/form/interview/InterviewStartController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      //技术经理面试任务处理页面
      $stateProvider.state("main.TM_INTERVIEW_TASK",{
          url: "/workflow/process/task/interview/tm/:id",
          controller:"tmInterviewTaskController",
          templateUrl:"view/workflow/form/interview/tm_interview_task.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/form/interview/TmInterviewTaskController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
      
      //项目经理面试任务处理页面
      $stateProvider.state("main.PM_INTERVIEW_TASK",{
          url: "/workflow/process/task/interview/pm/:id",
          controller:"pmInterviewTaskController",
          templateUrl:"view/workflow/form/interview/pm_interview_task.html",
          resolve: {
              deps:["$ocLazyLoad",function($ocLazyLoad){
                    return $ocLazyLoad.load("resources/js/controller/workflow/form/interview/pmInterviewTaskController.js");
                    }],
              service: ['$ocLazyLoad', function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            files: [MODAL_SERVICE,
                                    BLOCK_UI
                            ] 
                        });
              }]
          }
      });
    



}]);

colorAdminApp.controller('appController',['$rootScope', '$scope', function($rootScope, $scope) {
	 
}]);
